package handler

import (
	"errors"
	"net/http"
	"ruoyi-go-echo-ddd/internal/application"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/response"
	"ruoyi-go-echo-ddd/internal/infrastructure/utils"

	"github.com/labstack/echo/v4"
	"github.com/rs/zerolog"
	"github.com/spf13/cast"
)

// SysPostHandler 岗位信息控制器
type SysPostHandler struct {
	PostApp *application.SysPostApplication
}

// NewSysPostHandler 岗位信息控制器初始化
func NewSysPostHandler(app *application.SysPostApplication) *SysPostHandler {
	return &SysPostHandler{
		PostApp: app,
	}
}

// SaveHandler 新增岗位
// @Summary 新增岗位
// @Description 创建新的岗位信息
// @Tags 岗位管理
// @Accept application/json; charset=UTF-8
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param sysPost body model.SysPost true "岗位信息"
// @Success 200 {object} response.Result{data=model.SysPost} "新增成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Failure 500 {object} response.Result "服务器错误"
// @Router /system/post [post]
func (controller *SysPostHandler) SaveHandler(c echo.Context) error {
	var sysPost *model.SysPost
	if err := BindParam(c, &sysPost); err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("岗位信息新增参数解析错误")
		response.NewRespCodeMsg(c, 500, "岗位信息新增参数解析错误")
		return err
	}

	result, err := controller.PostApp.Insert(c, sysPost)
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}

	response.NewResponse(c, 200, "新增成功", result)
	return nil
}

// UpdateHandler 更新
// @Summary 修改岗位
// @Description 修改指定的岗位信息
// @Tags 岗位管理
// @Accept application/json; charset=UTF-8
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param sysPost body model.SysPost true "岗位信息"
// @Success 200 {object} response.Result{data=int64} "修改成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Failure 500 {object} response.Result "服务器错误"
// @Router /system/post [put]
func (controller *SysPostHandler) UpdateHandler(c echo.Context) error {
	var sysPost *model.SysPost
	if err := BindParam(c, &sysPost); err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("岗位修改参数解析错误")
		response.NewRespCodeMsg(c, 500, "岗位修改参数解析错误")
		return err
	}

	if sysPost.Id == 0 {
		response.NewRespCodeMsg(c, 500, "岗位信息ID不能为空")
		return errors.New("岗位信息ID不能为空")
	}

	result, err := controller.PostApp.Update(c, sysPost)
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}

	response.NewResponse(c, 200, "修改成功", result)
	return nil
}

// BatchSaveHandler 批量新增
// @Summary 批量新增岗位
// @Description 批量创建新的岗位信息
// @Tags 岗位管理
// @Accept application/json; charset=UTF-8
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param list body []model.SysPost true "岗位信息"
// @Success 200 {object} response.Result{data=[]int64} "批量新增成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Failure 500 {object} response.Result "服务器错误"
// @Router /system/post/batchSave [post]
func (controller *SysPostHandler) BatchSaveHandler(c echo.Context) error {
	var list []*model.SysPost
	if err := BindParam(c, &list); err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("岗位信息批量新增参数解析错误")
		response.NewRespCodeMsg(c, 500, "岗位信息批量新增参数解析错误")
		return err
	}

	result, err := controller.PostApp.BatchInsert(c, list)
	if err != nil {
		response.NewRespCodeErr(c, 500, err)
		return err
	}

	response.NewResponse(c, 200, "新增成功", result)
	return nil
}

type SysPostPage struct {
	Content []model.SysPost `json:"content"`
	Records int64           `json:"records,string"`
	Pages   int64           `json:"pages,string"`
}

// SelectPageHandler 分页处理器
// @Summary 岗位分页查询
// @Description 岗位分页查询
// @Tags 岗位管理
// @Accept application/json; charset=UTF-8
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param param body request.PostPageParam true "分页参数"
// @Success 200 {object} SysPostPage "查询成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Router /system/post/list [post]
func (controller *SysPostHandler) SelectPageHandler(c echo.Context) error {
	param := new(request.PostPageParam)
	if err := BindParam(c, param); err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("岗位分页参数解析错误===")
		response.NewRespCodeMsg(c, http.StatusInternalServerError, "岗位分页参数解析失败")
		return err
	}

	zerolog.DefaultContextLogger.Info().Msgf("岗位信息分页查询入参===\n%s", utils.ToJsonFormat(param))

	list, total, page, err := controller.PostApp.SelectPage(c, param)
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}

	pageData := response.NewPageData(list, total, page)

	response.NewResponse(c, 200, "查询成功", pageData)
	return nil
}

// SelectOneHandler 查询单个
// @Summary 岗位查询
// @Description 通过ID查询岗位
// @Tags 岗位管理
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param id path string true "岗位主键ID"
// @Success 200 {object} response.Result{data=model.SysPost} "查询成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Router /system/post/{id} [get]
func (controller *SysPostHandler) SelectOneHandler(c echo.Context) error {
	id, err := GetSinglePathParam(c, "id")
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}

	data, err := controller.PostApp.SelectById(c, cast.ToInt64(id))
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}

	response.NewResponse(c, 200, "查询成功", data)
	return nil
}

// BatchDeleteHandler 批量删除
// @Summary 岗位批量删除
// @Description 岗位批量删除
// @Tags 岗位管理
// @Accept application/json; charset=UTF-8
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Param ids body []any true "英文逗号分割的主键ID"
// @Success 200 {object} response.Result{data=int64} "删除成功"
// @Failure 500 {object} response.Result "参数解析错误"
// @Router /system/post/batchDelete [post]
func (controller *SysPostHandler) BatchDeleteHandler(c echo.Context) error {
	var ids []any
	if err := BindParam(c, &ids); err != nil {
		zerolog.DefaultContextLogger.Error().Err(err).Msg("岗位批量删除参数解析错误===")
		response.NewRespCodeErr(c, 500, err)
		return err
	}

	count, err := controller.PostApp.BatchDelete(c, ids)
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}
	response.NewResponse(c, 200, "批量删除成功", count)
	return nil
}

// SelectAllHandler 查询所有
// @Summary 查询岗位
// @Description 查询所有岗位
// @Tags 岗位管理
// @Produce application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Success 200 {object} response.Result{data=[]model.SysPost} "查询成功"
// @Router /system/post/selectAll [get]
func (controller *SysPostHandler) SelectAllHandler(c echo.Context) error {
	allPost, err := controller.PostApp.SelectAll(c)
	if err != nil {
		response.NewResponse(c, 500, err.Error(), nil)
		return err
	}
	response.NewResponse(c, 200, "查询成功", allPost)
	return nil
}

// ExportExcelBufferHandler 导出Excel，使用缓冲流
// @Summary 导出Excel
// @Description 导出Excel-使用缓冲流
// @Tags 岗位管理
// @Produce application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/json; charset=UTF-8
// @Security ApiKeyAuth
// @Success 200 {file} file "Excel文件"
// @Failure 500 {object} response.Result "参数解析错误"
// @Router /system/post/export [get]
func (controller *SysPostHandler) ExportExcelBufferHandler(c echo.Context) error {
	data, err := controller.PostApp.SelectAll(c)
	if err != nil {
		response.NewRespCodeErr(c, 500, err)
		return err
	}
	headers := []string{"字典类型ID", "岗位编码", "岗位名称", "显示顺序", "部门状态(0正常 1停用)", "创建人", "创建时间", "修改人", "修改时间", "备注信息"}
	return DownloadExcelBuffer(c, "岗位信息_"+utils.TimeNowFormat("")+".xlsx", "岗位信息", headers, data, true)
}
